package com.hsjk.szwj.module.train.mq.consumer;

import cn.hutool.core.util.ObjectUtil;
import com.hsjk.szwj.framework.mq.core.stream.AbstractStreamMessageListener;
import com.hsjk.szwj.module.train.dal.dataobject.mq.OrderFailureTimeMQ;
import com.hsjk.szwj.module.train.dal.dataobject.order.OrderDO;
import com.hsjk.szwj.module.train.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

/**
 * @author chenjiawei
 * @desc
 * @Date 2023/11/22 22:57
 **/
@Component
@Slf4j
public class OrderFailureTimeConsumer extends AbstractStreamMessageListener<OrderFailureTimeMQ> {
    @Resource
    private IOrderService orderService;

    @Value("${szwj.order.failure-time}")
    private String orderFailureTime;

    @Override
    public void onMessage(OrderFailureTimeMQ message) {
        log.info("OrderFailureTimeConsumer:{}", message);
        OrderDO orderDO = orderService.getOrderInfoByIdIgnoreTenant(message.getOrderId());
        if (ObjectUtil.isNotNull(orderDO)) {
            long betweenSs = ChronoUnit.SECONDS.between(LocalDateTime.now(), orderDO.getCreateTime());
            if (betweenSs > Long.valueOf(orderFailureTime)) {
                orderService.failureTimeCancelOrder(orderDO.getId());
            }
        }
    }
}
